//取出本地存储的数据
let token = JSON.parse(localStorage.getItem('token'));
(function() {
    if (token) {
        // $.ajax({
        //     url: 'http://www.liulongbin.top:3007/my/userinfo',
        //     method: 'GET',
        //     beforeSend: function(XMLHttpRequest) {
        //         XMLHttpRequest.setRequestHeader('Authorization', token)
        //     },
        //     success: function(data) {
        //         console.log(data.data)
        //         let userinfo = data.data
        //         if (userinfo.user_pic) {
        //             $('.layui-layout-right .layui-nav-img').attr('src', userinfo.user_pic)
        //             $('.layui-side-scroll .layui-nav-img').attr('src', userinfo.user_pic)
        //         }

        //         $('.layui-side-scroll .username').html(userinfo.username)
        //     }
        // })
        getUserInfo(token, function(userinfo) {
            if (userinfo.user_pic) {
                $('.layui-layout-right .layui-nav-img').attr('src', userinfo.user_pic)
                $('.layui-side-scroll .layui-nav-img').attr('src', userinfo.user_pic)
            }
            $('.layui-side-scroll .username').html(userinfo.username)
        }, function() {

        })

    } else {
        layer.msg('请先登录')
        setTimeout(() => {
            location.href = '/login.html'
        }, 2000);
    }
})();

// (function() {
//     $('.layui-nav .layui-nav-item a').click(function() {
//         localStorage.clear('token')
//         alert('退出成功')
//         $('.layui-side-scroll .username').html('请登录')
//         setTimeout(() => {
//             location.href = '/login.html'
//         }, 2000);
//     })
// })();

(function() {
    $(' .layui-nav-item .logout').click(function() {
        layer.confirm('确定退出？', { icon: 3, title: '提示' }, function(index) {
            layer.close(index)
            localStorage.removeItem('token')
            layer.msg('退出成功')
            $('.layui-side-scroll .username').html('请登录')
            setTimeout(() => {
                location.href = '/login.html'
            }, 2000);
        })
    })
})();

